package com.miyake.server.servlet;

import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.miyake.xml.MyXmlParser;
import com.miyake.xml.Parameter;

public class AnalysisServlet extends HttpServlet implements Servlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8722938596483907514L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		MyXmlParser parser = new MyXmlParser();
		String type = req.getParameter("type");
		if (type == null) {
			type = "";
		}
		if (type.equalsIgnoreCase("PON")) {
			parser.setName("Anritsu FAS for PON network");
			parser.setDescription("Auto Event Search Service for PON network");
			parser.getParameters().add(new Parameter("Splice", "Splice Loss Threshold", "Numeric", "0.1", "dB"));
			parser.getParameters().add(new Parameter("Reflect", "Splice Loss Threshold", "Numeric", "0.1", "dB"));
			parser.getParameters().add(new Parameter("End", "Splice Loss Threshold", "Numeric", "0.1", "dB"));
			parser.getParameters().add(new Parameter("Splitter", "Splitter Threshold", "Numeric", "0.1", "dB"));
			
		}
		else if (type.equalsIgnoreCase("Yokogawa")) {
			parser.setName("Yokogawa OTDR Analyzer");
			parser.getParameters().add(new Parameter("Splice", "Splice Loss Threshold", "Numeric", "0.1", "dB"));
			parser.getParameters().add(new Parameter("Reflect", "Splice Loss Threshold", "Numeric", "0.1", "dB"));
			parser.getParameters().add(new Parameter("End", "Splice Loss Threshold", "Numeric", "0.1", "dB"));
			parser.setDescription("Auto event detector service provided by Yokogawa");
		}
		else if (type.equalsIgnoreCase("EXFO")) {
			parser.setName("EXFO OTDR Analyzer");
			parser.getParameters().add(new Parameter("Splice", "Splice Loss Threshold", "Numeric", "0.1", "dB"));
			parser.getParameters().add(new Parameter("Reflect", "Splice Loss Threshold", "Numeric", "0.1", "dB"));
			parser.getParameters().add(new Parameter("End", "Splice Loss Threshold", "Numeric", "0.1", "dB"));
			parser.setDescription("Auto event detector service provided by EXFO");
		}
		else {
			parser.setName("Anritsu FAS");
			parser.setDescription("Auto Event Search Service");
			parser.getParameters().add(new Parameter("Splice", "Splice Loss Threshold", "Numeric", "0.1", "dB"));
			parser.getParameters().add(new Parameter("Reflect", "Reflect Threshold", "Numeric", "0.1", "dB"));
			parser.getParameters().add(new Parameter("End", "End Threshold", "Numeric", "0.1", "dB"));
			Parameter p = new Parameter("AutoPon", "Auto PON Mode", "Selection", "", "");
			p.getSelection().add("On");
			p.getSelection().add("Off");
			parser.getParameters().add(p);
		}
		parser.setHelpUrl("http://doctorssns.appspot.com");
		parser.setProvider("Anritsu Corporation");
		parser.setVersion("1.00");
		parser.setLicense("Commercial");
		
		resp.getWriter().print(parser.encode());
		resp.getWriter().flush();
		resp.getWriter().close();
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		MyXmlParser parser = new MyXmlParser();
		parser.decode(req.getInputStream());
		
//		parser.clear();
		
/*		for(int j = 0; j < 3; j++) {
			String[] out = new String[5];
			parser.setLength(out.length);
			for (Integer i = 0; i < out.length; i++) {
				out[i] = String.format("%.5e", Math.random());
			}
			parser.addY(out, "title");
		}
*/
		parser.setName("Event Table");
		resp.getWriter().print(parser.encode());
		resp.getWriter().flush();
		resp.getWriter().close();
	}

}
